<!-- 采购管理 采购到货 -->
<template>
<<<<<<< HEAD
  <view class="container">
=======
<<<<<<< HEAD
  <view class="container">
    <!-- 搜索表单 -->
    <van-form @submit="handleQuery" class="search-form">
      <van-field
        v-model="queryParams.arriveNo"
        label="到货单号"
        placeholder="请输入到货单号"
        label-width="6em"
      />
      <van-field
        v-model="queryParams.contractNo"
        label="合同编号"
        placeholder="请输入合同编号"
        label-width="6em"
      />
      <van-field
        v-model="queryParams.supplierId"
        is-link
        readonly
        label="供应商"
        placeholder="请选择供应商"
        @click="showSupplierPicker = true"
        label-width="6em"
      />
      <van-field
        v-model="dateRangeText"
        is-link
        readonly
        label="到货日期"
        placeholder="选择日期范围"
        @click="showDatePicker = true"
        label-width="6em"
      />
      <van-field
        v-model="queryParams.status"
        is-link
        readonly
        label="状态"
        placeholder="请选择状态"
        @click="showStatusPicker = true"
        label-width="6em"
      />
      <view class="form-buttons">
        <van-button round block type="primary" native-type="submit">搜索</van-button>
        <van-button round block @click="resetQuery">重置</van-button>
      </view>
    </van-form>

    <!-- 操作按钮 -->
    <view class="action-buttons">
      <van-button icon="plus" type="primary" @click="handleAdd">新增</van-button>
      <van-button icon="edit" @click="handleUpdate" :disabled="single">修改</van-button>
      <van-button icon="delete" @click="handleDelete" :disabled="multiple">删除</van-button>
    </view>

>>>>>>> b7382a76412d4256678c3302b3ac87f11fa1fe30
    <!-- 数据列表 -->
    <van-list
      v-model:loading="loading"
      :finished="finished"
      @load="getList"
<<<<<<< HEAD
    >
      <van-cell
        v-for="item in applyList"
        :key="item.id"
        :title="item.applyNo"
        :value="formatStatus(item.auditStatus)"
        clickable
        @click="handleUpdate(item)"
      >
        <view class="list-item">
          <view class="item-row">
            <van-tag type="primary">{{ formatType(item.applyType) }}</van-tag>
            <text class="plan-no">{{ item.planNo }}</text>
          </view>
          <view class="item-row">
            <text>{{ formatUser(item.applyUser) }}</text>
            <text>{{ formatTime(item.applyTime) }}</text>
          </view>
        </view>
      </van-cell>
    </van-list>
=======
      class="data-list"
    >
      <van-cell
        v-for="item in arriveList"
        :key="item.id"
        :title="item.arriveNo"
        :value="statusFormat(item.status)"
        is-link
        @click="handleUpdate(item)"
      >
        <template #label>
          <view class="list-item-content">
            <text>合同编号：{{ item.contractNo }}</text>
            <text>供应商：{{ item.supplierName }}</text>
            <text>到货日期：{{ formatDate(item.arriveDate) }}</text>
          </view>
        </template>
      </van-cell>
    </van-list>

    <!-- 新增/编辑弹窗 -->
    <van-popup
      v-model:show="showEditDialog"
      position="bottom"
      round
      :style="{ height: '90%' }"
    >
      <van-form @submit="submitForm">
        <van-field
          label="合同编号"
          v-model="form.contractNo"
          readonly
          right-icon="search"
          @click-right-icon="handleSelectContract"
        />
        <van-field
          label="到货日期"
          v-model="form.arriveDate"
          readonly
          placeholder="选择日期"
          @click="showArriveDatePicker = true"
        />
        <van-field
          label="备注"
          v-model="form.remark"
          type="textarea"
          autosize
        />

        <!-- 到货明细 -->
        <view class="detail-header">
          <text>到货明细</text>
          <van-button size="mini" @click="handleDeleteDetail">删除</van-button>
        </view>
        <van-checkbox-group v-model="checkedDetails">
          <view
            v-for="(detail, index) in scmPurchaseArriveDetailList"
            :key="index"
            class="detail-item"
          >
            <van-checkbox :name="index" />
            <view class="detail-content">
              <text>{{ detail.materialName }} {{ detail.materialSpecification }}</text>
              <van-field
                v-model="detail.batchNo"
                label="生产批号"
                placeholder="请输入"
                :rules="[{ required: true }]"
              />
              <van-field
                v-model="detail.arriveQuantity"
                label="到货数量"
                placeholder="请输入"
                type="digit"
                :rules="[{ required: true }]"
              />
            </view>
          </view>
        </van-checkbox-group>

        <view class="dialog-buttons">
          <van-button round block type="primary" native-type="submit">提交</van-button>
          <van-button round block @click="cancel">取消</van-button>
        </view>
      </van-form>
    </van-popup>

    <!-- 合同选择弹窗 -->
    <contract-select
      :show="showContractSelect"
      @close="showContractSelect = false"
      @select="handleContractSelected"
    />
  </view>
</template>

<script setup>
import { ref, reactive, computed } from 'vue';


// 状态字典
const statusOptions = ref([
  { label: '待入库', value: '0' },
  { label: '已入库', value: '1' }
]);

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  arriveNo: '',
  contractNo: '',
  supplierId: null,
  status: null
});

// 表单数据
const form = reactive({
  id: null,
  contractNo: '',
  arriveDate: '',
  remark: '',
  scmPurchaseArriveDetailList: []
});

// 模拟接口
const mockApi = {
  getList: () => ({
    rows: [
      {
        id: 1,
        arriveNo: 'DH202307001',
        contractNo: 'HT202307001',
        supplierName: 'XX供应商',
        arriveDate: '2023-07-15',
        status: '0',
        scmPurchaseArriveDetailList: [
          {
            materialName: '光伏组件',
            materialSpecification: '550W',
            batchNo: 'B20230701',
            arriveQuantity: 100
          }
        ]
      }
    ],
    total: 1
  })
};

// 列表数据
const arriveList = ref([]);
const loading = ref(false);
const finished = ref(false);

// 获取列表
const getList = async () => {
  loading.value = true;
  try {
    const res = await mockApi.getList(queryParams);
    arriveList.value = res.rows;
    finished.value = true;
  } finally {
    loading.value = false;
  }
};

// 状态格式化
const statusFormat = (status) => {
  return statusOptions.value.find(item => item.value === status)?.label || '';
};

// 其他业务逻辑（查询、重置、新增、删除等）与之前类似
// 样式部分需要添加双碳主题颜色
</script>

<style lang="scss">
.container {
  background: #f5f5f5;
  min-height: 100vh;
  padding: 20rpx;
  
  .van-button--primary {
    background: #07c160;
    border-color: #07c160;
  }
  
  .detail-header {
    display: flex;
    justify-content: space-between;
    padding: 20rpx;
    background: #e8f5e9;
  }
  
  .detail-item {
    display: flex;
    align-items: center;
    padding: 20rpx;
    background: white;
    margin-bottom: 10rpx;
    
    .detail-content {
      flex: 1;
      margin-left: 20rpx;
    }
  }
}
</style>
=======
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="到货单号" prop="arriveNo">
        <el-input v-model="queryParams.arriveNo" placeholder="请输入到货单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="合同编号" prop="contractNo">
        <el-input v-model="queryParams.contractNo" placeholder="请输入合同编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商" prop="supplierId">
        <el-select v-model="queryParams.supplierId" placeholder="请选择供应商" clearable filterable >
          <el-option v-for="dict in supplierIdOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="到货日期">
        <el-date-picker v-model="daterangeArriveDate"  style="width: 240px" value-format="yyyy-MM-dd"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable filterable >
          <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="daterangeCreateTime"  style="width: 240px" value-format="yyyy-MM-dd"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button  type="primary"   @click="handleQuery">搜索</el-button>
        <el-button    @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
>>>>>>> b7382a76412d4256678c3302b3ac87f11fa1fe30

    <!-- 表单弹窗 -->
    <van-popup
      v-model:show="showForm"
      position="bottom"
      round
      :style="{ height: '90%' }"
    >
      <view class="form-container">
        <van-form @submit="saveForm">
          <van-cell title="申请单号">
            <van-field
              v-model="form.applyNo"
              placeholder="系统自动生成"
              readonly
            />
          </van-cell>

          <van-cell title="计划单号" clickable @click="showPlanSelect = true">
            <van-field
              v-model="form.planNo"
              placeholder="点击选择计划"
              readonly
              :right-icon="form.planNo ? '' : 'arrow'"
            />
          </van-cell>
          <van-divider>采购明细</van-divider>

          <view class="detail-actions">
            <van-button
              size="small"
              type="primary"
              icon="plus"
              @click="handleAddDetail"
            >添加物料</van-button>
          </view>
          <view v-for="(detail, index) in detailList" :key="index" class="detail-item">
            <van-cell
              :title="`物料 ${index + 1}`"
              clickable
              @click="openMaterialSelect(index)"
            >
              <template #right-icon>
                <van-icon
                  name="delete"
                  class="delete-icon"
                  @click="handleDeleteDetail(index)"
                />
              </template>
            </van-cell>
            <van-field
              v-model="detail.materialName"
              label="物料名称"
              readonly
              placeholder="点击选择物料"
            />
            <van-field v-model="detail.price" label="单价" type="digit" />
            <van-field v-model="detail.quantity" label="采购数量" type="digit" />
          </view>
          <view class="form-actions">
            <van-button block type="primary" native-type="submit">保存</van-button>
            <van-button block type="warning" @click="submitForm">提交审核</van-button>
            <van-button block @click="showForm = false">取消</van-button>
          </view>
        </van-form>
      </view>
    </van-popup>

    <!-- 选择物料弹窗 -->
    <van-popup v-model:show="showMaterialSelect" position="bottom">
      <van-picker
        :columns="materialOptions"
        @confirm="onMaterialSelect"
        @cancel="showMaterialSelect = false"
      />
    </van-popup>
  </view>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue';

// 添加缺失的数据定义
const applyTypeOptions = [
  { value: 1, label: '常规采购' },
  { value: 2, label: '紧急采购' },
  { value: 3, label: '战略采购' }
];

// 定义响应式变量
const showMaterialSelect = ref(false);
const currentMaterialIndex = ref(null);

// 修改函数名以避免冲突
const openMaterialSelect = (index) => {
  currentMaterialIndex.value = index;
  showMaterialSelect.value = true;
};

// 修复表单提交逻辑
const saveForm = () => {
  if (!form.id) {
    form.id = Date.now();
    form.details = detailList.value;
    applyList.value = [form, ...applyList.value];
  }
  showForm.value = false;
  uni.showToast({ title: '保存成功', icon: 'success' });
};

// 修复加载更多逻辑
const getList = async () => {
  if (loading.value || finished.value) return;

  loading.value = true;
  await new Promise(resolve => setTimeout(resolve, 500));

  const start = (queryParams.pageNum - 1) * queryParams.pageSize;
  const end = queryParams.pageNum * queryParams.pageSize;

  const filtered = mockData.applyList.filter(item => {
    const noMatch = item.applyNo.includes(queryParams.applyNo);
    const typeMatch = !queryParams.applyType || item.applyType === queryParams.applyType;
    return noMatch && typeMatch;
  });

  applyList.value = applyList.value.concat(filtered.slice(start, end));
  queryParams.pageNum++;

  loading.value = false;
  finished.value = applyList.value.length >= filtered.length;
};

// 初始化时加载数据
onMounted(() => {
  getList();
});
</script>
<<<<<<< HEAD

<style scoped>
/* 修复样式问题 */
.container {
  padding: 20rpx;
  min-height: 100vh;
  box-sizing: border-box;
}

.list-item {
  padding: 16rpx 0;
  
  .item-row {
    display: flex;
    justify-content: space-between;
    margin: 8rpx 0;
    font-size: 24rpx;
    color: #666;
  }
}

.detail-item {
  margin: 20rpx;
  padding: 20rpx;
  background: #f5f5f5;
  border-radius: 12rpx;
}

.form-actions {
  margin-top: 40rpx;
  
  .van-button {
    margin: 20rpx 0;
  }
}
</style>
=======
>>>>>>> 5b8858b09e35d379956227f2720ff4c2cd0dbd08
>>>>>>> b7382a76412d4256678c3302b3ac87f11fa1fe30
